package scala.test

import scala.io.Source

/**
  * 统计班级人数
  */
object Test01 {
  def main(args: Array[String]): Unit = {
    //读数据
    val students: List[String] = Source.fromFile("data/students.txt").getLines().toList
    //取数据
    val clazzAndname: List[(String, String)] = students.map((student:String) =>{
      val split: Array[String] = student.split(",")
      val name: String = split(1)
      val clazz: String = split(4)
      (clazz,name)
    })
    //根据班级分组
    val group: Map[String, List[(String, String)]] = clazzAndname.groupBy((kv:(String,String)) =>kv._1)
    //计算班级的人数
    val clazzAndNum: Map[String, Int] = group.map((kv:(String,List[(String,String)])) =>{
      val clazz: String = kv._1
      val names: List[(String, String)] = kv._2
      val num: Int = names.length
      //返回一个二元组
      (clazz,num)
    })
    clazzAndNum.foreach(println)
  }
}
